home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 051-075 / disk_069 / console / console.c next >
C/C++ Source or Header  |  1992-05-06  |  10KB  |  415 lines

  1. #include <exec/types.h>
  2. #include <exec/io.h>
  3. #include <intuition/intuition.h>
  4. #include <devices/conunit.h>
  5.  
  6. #define EOF -1
  7. #define GRAPHICS_OLDREV  29
  8. #define INTUITION_OLDREV 29
  9. #define GRAPHICS_REV     33
  10. #define INTUITION_REV     33
  11.  
  12. #define INTUITION_OPEN 1
  13. #define GRAPHICS_OPEN  INTUITION_OPEN << 1
  14. #define WINDOW_OPEN    GRAPHICS_OPEN  << 1
  15. #define TMPRAS_OPEN    WINDOW_OPEN    << 1
  16. #define READPORT_OPEN  TMPRAS_OPEN    << 1
  17. #define CONREADER_OPEN READPORT_OPEN  << 1
  18. #define CONDEVICE_OPEN CONREADER_OPEN << 1
  19.  
  20. #define RP mywindow->RPort    /* make life easy, since I use it a lot */
  21.  
  22. void myexit(), mysetup(), myprintf(), queueread();
  23. int mygetc(), myscanf();
  24. char *mygets();
  25.  
  26. extern struct IntuitionBase *IntuitionBase;
  27. struct GfxBase *GfxBase;
  28.  
  29. static struct IOStdReq *conreader, conrequest;
  30. static struct MsgPort *conreadport;
  31. struct Window *mywindow;
  32. static struct TmpRas TRas;
  33. static PLANEPTR TBuf;
  34. extern char WindowTitle[];
  35. extern char FillFlag;
  36. static char buff[513];
  37. static char conchar;
  38. static int myflags;
  39.  
  40. char FillFlag;            /* whether or not to fill graphic objects */
  41. static char Color;        /* drawing/filling color */
  42. static char OldFlag;        /* using graphics.library before 1.2 */
  43.  
  44. static struct NewWindow NewWindow = {
  45.    0,1,640,199,-1,-1,        /* left, top, width, height, detail, block pen */
  46.    CLOSEWINDOW,
  47.    WINDOWDEPTH | WINDOWDRAG | ACTIVATE,
  48.    NULL, NULL, WindowTitle,
  49.    NULL, NULL, 0, 0, 0, 0,    /* Min & Max size don't matter with no size gadget */
  50.    WBENCHSCREEN };
  51.  
  52. /* initialize the window so we can read and write to it */
  53. void mysetup()
  54. {
  55.    OldFlag = 0;
  56.  
  57.    if ((IntuitionBase = (struct IntuitionBase *)
  58.       OpenLibrary("intuition.library", INTUITION_REV)) == NULL)
  59.     {
  60.     if ((IntuitionBase = (struct IntuitionBase *)
  61.        OpenLibrary("intuition.library", INTUITION_OLDREV)) == NULL)
  62.        myexit(1);
  63.     else OldFlag = 1;
  64.     }
  65.  
  66.    myflags |= INTUITION_OPEN;
  67.  
  68.    if (OldFlag)
  69.     {
  70.     if ((GfxBase = (struct GfxBase *)
  71.        OpenLibrary("graphics.library", GRAPHICS_OLDREV)) == NULL)
  72.        myexit(2);
  73.     }
  74.    else
  75.     {
  76.     if ((GfxBase = (struct GfxBase *)
  77.        OpenLibrary("graphics.library", GRAPHICS_REV)) == NULL)
  78.        myexit(2);
  79.     }
  80.  
  81.    myflags |= GRAPHICS_OPEN;
  82.  
  83.    if ((mywindow = (struct Window *)OpenWindow(&NewWindow)) == NULL)
  84.       myexit(3);
  85.  
  86.    myflags |= WINDOW_OPEN;
  87.  
  88. /* create a TmpRas so we can do FloodFill calls */
  89.  
  90.    if ((TBuf = (PLANEPTR)AllocRaster(640,200)) == NULL)
  91.       myexit(4);
  92.    RP->TmpRas = (struct TmpRas *)InitTmpRas(&TRas,TBuf,RASSIZE(640,200));
  93.  
  94.    myflags |= TMPRAS_OPEN;
  95.  
  96. /* create a port to receive messages about completed operations */
  97.    if ((conreadport = CreatePort("my.con.read", 0)) == NULL)
  98.       myexit(5);
  99.  
  100.    myflags |= READPORT_OPEN;
  101.  
  102. /* construct a request for that port */
  103.    if ((conreader = CreateStdIO(conreadport)) == NULL)
  104.       myexit(6);
  105.  
  106.    myflags |= CONREADER_OPEN;
  107.  
  108.    conreader->io_Data = (APTR)mywindow;
  109.    conreader->io_Length = sizeof(*mywindow);
  110.  
  111.    if (OpenDevice("console.device", 0, conreader, 0) != 0)
  112.       myexit(7);
  113.  
  114.    myflags |= CONDEVICE_OPEN;
  115.  
  116. /* copy so we have a read request structure */
  117.    conrequest.io_Device = conreader->io_Device;
  118.    conrequest.io_Unit = conreader->io_Unit;
  119.  
  120.    queueread();
  121. }
  122.  
  123. void myputc(c)    /* write a single character to our window */
  124. char c;
  125. {
  126.    if (mywindow == NULL) mysetup();
  127.  
  128.    conrequest.io_Command = CMD_WRITE;
  129.    conrequest.io_Data = (APTR)&c;
  130.    conrequest.io_Length = 1;
  131.    DoIO(&conrequest);
  132. }
  133.  
  134. void queueread()
  135. {
  136.    conreader->io_Command = CMD_READ;
  137.    conreader->io_Data = &conchar;
  138.    conreader->io_Length = 1;
  139.    SendIO(conreader);
  140. }
  141.  
  142. int mygetc()
  143. {
  144.    struct IntuiMessage *GetMsg();
  145.    char c = 0;
  146.  
  147.    if (mywindow == NULL) mysetup();
  148.  
  149. /* if there hasn't been a response to the last read request, wait for it */
  150.    while((GetMsg(conreadport) == NULL)) WaitPort(conreadport);
  151. /* get what the response character was */
  152.    c = conchar;
  153. /* and queue up for another one */
  154.    queueread();
  155.    return((c == 0x1c) ? EOF : c);
  156. }
  157.  
  158. char *mygets()
  159. {
  160.    static char DELCHR[] = "\b \b";    /* String to DELete a CHaRacter */
  161.    register char inchar;
  162.    register int i,j;
  163.    register int count = 0;
  164.    register int ScrCount;
  165.    int LnCount, MaxLnCount = 0;
  166.    int InitCount = 0;
  167.  
  168.    if (mywindow == NULL) mysetup();
  169.  
  170.    myprintf("\x9b6n");            /* <CSI>6n - get current cursor pos */
  171.    while (mygetc() != ';');        /*   returned as "<CSI>row;columnR" */
  172.                     /*   row & column returned as ASCII */
  173.    while ((inchar = mygetc()) != 'R')
  174.       InitCount = (InitCount * 10) + (inchar - '0');
  175.  
  176.    LnCount = ScrCount = InitCount;
  177.  
  178.    myprintf("\x9b q");            /* <CSI><space>q - window status request */
  179.    for (i=0;i<3;i++)            /* returns window bounds in printable */
  180.       while (mygetc() != ';');        /* characters, format:             */
  181.                     /* <CSI>top line,first char,bottom line,last char<space>r */
  182.    while ((inchar = mygetc()) != ' ')
  183.       MaxLnCount = (MaxLnCount * 10) + (inchar - '0');
  184.  
  185.    mygetc();                /* throw away the 'r' */
  186.  
  187.    buff[0] = NULL;
  188.  
  189.    do {
  190.       switch (inchar = mygetc()) {
  191.      case '\b' :            /* Backspace handling */
  192.         if (count == 0)
  193.            break;
  194.         if (buff[--count] == '\t') {
  195.            i = buff[--count];
  196.            ScrCount -= i;
  197.            LnCount = ((LnCount - i) >= 0) ? (MaxLnCount - i) : (LnCount - i);
  198.            for(j=i;j>0;j--)
  199.           myprintf("%s",DELCHR);
  200.         } else {
  201.            ScrCount--;
  202.            LnCount = ((LnCount - 1) >= 0) ? MaxLnCount : 0;
  203.            myprintf("%s", DELCHR);    /* Got to use myprintf to avoid newline */
  204.         }
  205.         break;
  206.      case '\t' :            /* tab character */
  207.         if (count < 256) {
  208.            i = 9-(LnCount%8);
  209.            if ((LnCount + i) > MaxLnCount) {
  210.           i = MaxLnCount - LnCount + 1;
  211.           LnCount = 1;
  212.            } else LnCount += i;
  213.            ScrCount += i;
  214.            buff[count++] = i;
  215.            buff[count++] = '\t';
  216.            for(j=0;j<i;j++) myputc(' ');
  217.         }
  218.         break;
  219.      case '\r' :
  220.         buff[count] = '\r';
  221.         myputc('\n');
  222.         break;
  223.      case 0x18 :            /* This is handling of ^X */
  224.         if (count == 0)
  225.            break;
  226.         myprintf("\x9b0 p");    /* turn cursor off */
  227.         for (; ScrCount > InitCount; ScrCount--)
  228.            myprintf("%s", DELCHR);
  229.         myprintf("\x9b p");        /* turn cursor on */
  230.         count = 0;
  231.         LnCount = InitCount;
  232.         buff[0] = NULL;
  233.         break;
  234.      default :
  235.         if ((count < 256) && (inchar > 31)) {
  236.            myputc(inchar);
  237.            ScrCount++;
  238.            LnCount = ((LnCount + 1) > MaxLnCount) ? 0 : (LnCount + 1);
  239.            buff[count++] = inchar;
  240.         }
  241.       }
  242.    } while (inchar != '\r');
  243.  
  244.    buff[count] = '\0';
  245.    for(i=count-1;i>0;i--)
  246.       if(buff[i] == '\t') {
  247.      for(j=i-1;j<count;j++)
  248.         buff[j] = buff[j+1];
  249.          i--;
  250.       }
  251.    return(buff);
  252. }
  253.  
  254. int myscanf(str,v1,v2,v3,v4,v5,v6,v7,v8,v9)
  255. char *str;
  256. long *v1,*v2,*v3,*v4,*v5,*v6,*v7,*v8,*v9;
  257. {
  258.    return(sscanf(mygets(),str,v1,v2,v3,v4,v5,v6,v7,v8,v9));
  259. }
  260.  
  261. void myputs(str)    /* write a string to our window */
  262. char *str;
  263. {
  264.    if (mywindow == NULL) mysetup();
  265.  
  266.    conrequest.io_Command = CMD_WRITE;
  267.    conrequest.io_Data = (APTR)str;
  268.    conrequest.io_Length = strlen(str);
  269.    DoIO(&conrequest);
  270.    myputc('\n');    /* don't forget the newline for the string */
  271. }
  272.  
  273. /* write a formatted string to our window */
  274. void myprintf(str,v1,v2,v3,v4,v5,v6,v7,v8,v9)
  275. char *str;
  276. long v1,v2,v3,v4,v5,v6,v7,v8,v9;
  277. {
  278.    char buff[256];
  279.  
  280.    if (mywindow == NULL) mysetup();
  281.  
  282.    conrequest.io_Command = CMD_WRITE;
  283.    conrequest.io_Data = (APTR)buff;
  284.    conrequest.io_Length = sprintf(buff,str,v1,v2,v3,v4,v5,v6,v7,v8,v9);
  285.    DoIO(&conrequest);
  286. }
  287.  
  288. /* close up everything and leave the program */
  289. void myexit(code)
  290. int code;
  291. {
  292.    if (myflags && CONDEVICE_OPEN) CloseDevice(conreader);
  293.    if (myflags && CONREADER_OPEN) DeleteStdIO(conreader);
  294.    if (myflags && READPORT_OPEN) DeletePort(conreadport);
  295.    if (myflags && TMPRAS_OPEN) FreeRaster(TBuf,640,200);
  296.    if (myflags && WINDOW_OPEN) CloseWindow(mywindow);
  297.    if (myflags && GRAPHICS_OPEN) CloseLibrary(GfxBase);
  298.    if (myflags && INTUITION_OPEN) CloseLibrary(IntuitionBase);
  299.    _exit(code);
  300. }
  301.  
  302. /****************************************************************
  303.  *                                *
  304.  *        Utility Graphics Routines            *
  305.  *    Copyright (C) 1986 by Limited Reality, Inc.        *
  306.  *                                *
  307.  ****************************************************************/
  308.  
  309. Line(x1,y1,x2,y2,c)    /* draw a line from (x1,y1) to (x2,y2) */
  310. int x1,y1,x2,y2;
  311. char c;
  312. {
  313.     if(c != Color) SetAPen(RP,(Color = c));
  314.     Move(RP,x1,y1);
  315.     Draw(RP,x2,y2);
  316. }
  317.  
  318. Circle(cx,cy,r,c)    /* draw a circle with center at (cx,cy), radius r */
  319. int cx,cy,r;
  320. char c;
  321. {
  322.     if(c != Color) SetAPen(RP,(Color = c));
  323.     if (OldFlag)        /* if you're using something older than 1.2 */
  324.         {
  325.         int dx,dy,change,delta;
  326.  
  327.         dx = 0;
  328.         dy = r;
  329.  
  330.         change = 2-2*dy;
  331.  
  332.         while (dy >= 0)
  333.             {
  334.             WritePixel(RP,cx+dx,cy-(dy/2));
  335.             WritePixel(RP,cx+dy,cy+(dx/2));
  336.             WritePixel(RP,cx-dx,cy+(dy/2));
  337.             WritePixel(RP,cx-dy,cy-(dx/2));
  338.  
  339.             if (change <= 0)
  340.                 {
  341.                 delta = 2*change+2*dy-1;
  342.                 dx++;
  343.                 change = change+2*dx+1;
  344.                 if (delta > 0)
  345.                     {
  346.                     dy--;
  347.                     change = change-2*dy+1;
  348.                     }
  349.                 }
  350.             else
  351.                 {
  352.                 delta = 2*change-2*dx-1;
  353.                 dy--;
  354.                 change = change-2*dy+1;
  355.                 if (delta <= 0)
  356.                     {
  357.                     dx++;
  358.                     change = change+2*dx+1;
  359.                     }
  360.                 }
  361.             }
  362.         }
  363.         if (FillFlag) Fill(cx,cy,c);
  364.     else
  365.         {
  366.         if (FillFlag)
  367.             AreaEllipse(RP,cx,cy,r,r/2);
  368.         else
  369.             DrawEllipse(RP,cx,cy,r,r/2);
  370.         }
  371. }
  372.  
  373. Box(tlx,tly,brx,bry,c)    /* draw a box with top left corner at (tlx,tly) */
  374. int tlx,tly,brx,bry;    /*        and bottom right corner at (brx,bry) */
  375. char c;
  376. {
  377.     if(c != Color) SetAPen(RP,(Color = c));
  378.     if (FillFlag)
  379.         {
  380.         RectFill(RP,tlx,tly,brx,bry);
  381.         }
  382.     else
  383.         {
  384.         Move(RP,tlx,tly);
  385.         Draw(RP,tlx,bry);
  386.         Draw(RP,brx,bry);
  387.         Draw(RP,brx,tly);
  388.         Draw(RP,tlx,tly);
  389.         }
  390. }
  391.  
  392. Fill(x,y,c)        /* flood fill an area with (x,y) as the center */
  393. SHORT x,y;
  394. char c;
  395. {
  396.     if(c != Color) SetAPen(RP,(Color = c));
  397.     Flood(RP,1,x,y);
  398. }
  399.  
  400. PSet(x,y,c)        /* set point at (x,y) */
  401. int x,y;
  402. char c;
  403. {
  404.     if(c != Color) SetAPen(RP,(Color = c));
  405.     WritePixel(RP,x,y);
  406. }
  407.  
  408. PReset(x,y)        /* reset a point at (x,y) to background color */
  409. int x,y;
  410. {
  411.     SetAPen(RP,0);
  412.     WritePixel(RP,x,y);
  413.     Color = 0;
  414. }
  415.